package com.yocard.yxyd.ui.presenter;

import android.app.Activity;
import android.os.Bundle;

import com.alipay.sdk.app.PayTask;
import com.yocard.yxyd.R;
import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.response.RechargesBean;
import com.yocard.yxyd.http.model.response.TopOrderBean;
import com.yocard.yxyd.http.model.response.UserInfoBean;
import com.yocard.yxyd.others.PayResult;
import com.yocard.yxyd.ui.activity.PayResulActivity;
import com.yocard.yxyd.ui.contract.TopUpContract;
import com.yocard.yxyd.utils.RxUtil;
import com.yocard.yxyd.utils.util.ToastUtils;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import lombok.ToString;

/**
 * Created on 2019/4/9.
 */
public class TopupPresenter extends RxPresenter<TopUpContract.View> implements TopUpContract.Presenter {

    @Inject
    public TopupPresenter(HttpManager manager) {
        super(manager);
    }


    @Override
    public void getRecharges() {
        addSubscribe(httpManager.getRecharges()
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<RechargesBean>>(mView) {
                    @Override
                    public void onSuccess(List<RechargesBean> data) {
                        mView.getRechargesSuccess(data);
                    }
                }));
    }

    @Override
    public void createOrder(Activity activity,String id) {
        AndPermission.with(AppContext.getContext())
                .runtime()
                .permission(Permission.READ_PHONE_STATE, Permission.WRITE_EXTERNAL_STORAGE)
                .onGranted(permissions -> {
                    createOrderInner(activity,id);
                })
                .onDenied(permissions -> {
                    createOrderInner(activity,id);
                })
                .start();
    }

    private void createOrderInner(Activity activity,String id) {
        addSubscribe(httpManager.createOrder(id)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<TopOrderBean>(mView) {
                    @Override
                    public void onSuccess(TopOrderBean data) {
                        doPay(activity,data.getAlipay_response());
                    }
                }));
    }

    @Override
    public void getBalance() {
        addSubscribe(httpManager.getUswrInfo()
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UserInfoBean>(mView) {

                    @Override
                    public void onSuccess(UserInfoBean data) {
                        mView.onGetBalance(data.getCoin_balance());
                    }
                }));
    }

    public void doPay(Activity activity,String alipayInfo) {
        addSubscribe(Observable.create(new ObservableOnSubscribe<PayResult>() {
            @Override
            public void subscribe(ObservableEmitter<PayResult> emitter) throws Exception {
                PayTask alipay = new PayTask(activity);
                Map<String, String> result = alipay.payV2(alipayInfo, true);
                PayResult payResult = new PayResult(result);
                emitter.onNext(payResult);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result->{
                    Bundle bundle = new Bundle();
                    if("5000".equals(result.getResultStatus())){
                        ToastUtils.showShort(R.string.double_pay_tips);
                        return;
                    }else if("6001".equals(result.getResultStatus())) {
                        ToastUtils.showShort(R.string.cancel_pay_tips);
                        return;
                    }else if("6002".equals(result.getResultStatus())) {
                        ToastUtils.showShort(R.string.network_error_tips);
                        return;
                    }else if("4000".equals(result.getResultStatus())) {
                        bundle.putBoolean("isPaySuccess",false);
                        bundle.putString("msg", result.getMemo());
                        ActivityUtils.startActivity(bundle, PayResulActivity.class);
                    }else if("9000".equals(result.getResultStatus())) {
                        bundle.putBoolean("isPaySuccess",true);
                        ActivityUtils.startActivity(bundle, PayResulActivity.class);
                    }else {
                        ToastUtils.showShort(result.getMemo());
                    }
                }));
    }
}
